package com.tiger.contact;

import java.util.ArrayList;
import java.util.List;

import com.tiger.contact.MainActivity.ContactAdapter;
import com.tiger.contact.MainActivity.ContactInfo;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Message;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 通讯录处理工具
 * tiger 
 * 2011-4-29 上午10:08:41
 */
public class ContactUtil {
public static ContactUtil service;
	
	private ContactUtil(){
		
	}
	
	public static ContactUtil getInstance(){
		if(service == null)
			service = new ContactUtil();
		return service;
	}
	/**
	 * 删除联系人
	 * tiger 
	 * 2011-4-29 上午10:21:37
	 * @param activity
	 * @param position
	 * @return
	 */
	public int deleteSelectedItem(Activity activity,ContactEntity entity,String type){
		Uri uri = null;
		String whereSql=null;
		if("sim".equals(type)){
			Intent intent = new Intent();
			intent.setData(Uri.parse("content://icc/adn"));
			uri = intent.getData();
			whereSql=People._ID+"="+entity.getId();
		}else{
			uri = ContentUris.withAppendedId(People.CONTENT_URI, entity.getId());
		}
		Log.i("MMALL", "sim id:"+whereSql);
		int count = activity.getContentResolver().delete(uri, whereSql, null);
		return count;
	}
	/**
	 * 批量删除
	 * tiger 
	 * 2011-4-29 上午10:43:00
	 * @param activity
	 * @param entitys
	 * @return
	 */
	public int deleteSelectedItems(Activity activity,List<ContactEntity> entitys,String type){
		int num=0;
		for(ContactEntity entity:entitys){
			num+=deleteSelectedItem(activity,entity,type);
		}
		return num;
	}
	/**
	 * 读取联系人
	 * tiger 
	 * 2011-4-29 上午10:08:32
	 * @param activity
	 * @param type
	 * @return
	 */
	public List<ContactEntity> loadInfoFromPhone(Activity activity,String type) {
		List<ContactEntity> list = new ArrayList<ContactEntity>();
		Cursor cursor;
		Uri uri;
		if ("sim".equals(type)) {
			Intent intent = new Intent();
			intent.setData(Uri.parse("content://icc/adn"));
			uri = intent.getData();
		} else
			uri = People.CONTENT_URI;

		cursor = activity.getContentResolver().query(uri, null, null, null, null);
		ContactEntity entity;
		while (cursor.moveToNext()) {
			entity = new ContactEntity();
			int peopleId = cursor.getColumnIndex(People._ID);
			int nameId = cursor.getColumnIndex(People.NAME);
			int phoneId = cursor.getColumnIndex(People.NUMBER);

			entity.setPhoneNum(cursor.getString(phoneId));
			if(entity.getPhoneNum()==null||entity.getPhoneNum().length()==0)
				continue;
			entity.setId(cursor.getLong(peopleId));
			entity.setName(cursor.getString(nameId));
			entity.setChecked(false);
			list.add(entity);
		}
		return list;
	}
	/**
	 * 新增sim卡联系人
	 * @author:tiger.chenlin (2011-5-5) 
	 * @file:ContactUtil.java 
	 * @return:int
	 * @param activity
	 * @param contact
	 * @return
	 */
	public int insertSimContact(Activity activity,ContactEntity contact){
		try{
			Uri uri;
			Intent intent = new Intent();
			intent.setData(Uri.parse("content://icc/adn"));
			uri = intent.getData();
			ContentValues values = new ContentValues();
			values.put(People.NAME, contact.getName());
			values.put(People.NUMBER, contact.getPhoneNum());
			values.put(People.TYPE, People.TYPE_MOBILE);
		       activity.getContentResolver().insert(uri, values);
		}catch(Exception exp){
			exp.printStackTrace();
			return -1;
		}
		return 0;
	}
	/**
	 * 新增联系人
	 * @author:tiger.chenlin (2011-5-4) 
	 * @file:ContactUtil.java 
	 * @return:int
	 * @param activity
	 * @param contact
	 * @return
	 */
	public int insert(Activity activity,ContactEntity contact){
		try{
			ContentValues values = new ContentValues();
		       values.put(People.NAME, contact.getName());
		       Uri uri = activity.getContentResolver().insert(People.CONTENT_URI, values);
		       Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
		       values.clear();
		       
		       values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
		       values.put(People.NUMBER, contact.getPhoneNum());
		       activity.getContentResolver().insert(numberUri, values);
		}catch(Exception exp){
			exp.printStackTrace();
			return -1;
		}
		return 0;
	}
	public void checkAll(Activity activity,List<ContactEntity> contactData,
			List<ContactEntity> contactSelected,TextView deal_num,
			ContactAdapter adapter) {
		for(int i=0;i<contactData.size()&&contactData!=null;i++){
			contactData.get(i).setChecked(true);
		}
		contactSelected.clear();
		contactSelected=contactData;
		adapter.notifyDataSetChanged();
		deal_num.setText("已选择"+contactSelected.size()+"个联系人");
	}

	public void cancelAll(Activity activity,List<ContactEntity> contactData,
			List<ContactEntity> contactSelected,TextView deal_num,ContactAdapter adapter) {
		for(int i=0;i<contactData.size()&&contactData!=null;i++){
			contactData.get(i).setChecked(false);
		}
		contactSelected.clear();
		adapter.notifyDataSetChanged();
		deal_num.setText("已选择"+contactSelected.size()+"个联系人");
	}
	public boolean bakContact(){
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public boolean restoreContact(){
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
}
